package cn.dg.manager.order.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;

import cn.dg.manager.base.BaseController;
import cn.dg.manager.base.entity.HttpResult;
import cn.dg.manager.base.util.PageData;
import cn.dg.manager.common.entity.GoodsComboboxBean;
import cn.dg.manager.common.entity.PageEntity;
import cn.dg.manager.order.entity.OrderDetailResultBean;
import cn.dg.manager.order.entity.OrderEditPageBean;
import cn.dg.manager.order.entity.OrderInfoResultBean;
import cn.dg.manager.order.entity.OrderPageParam;
import cn.dg.manager.order.service.OrderService;

@Controller
public class OrderAction extends BaseController {

	@Autowired
	private OrderService orderService;
	
	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value="/order/index")
	public String index() {
		return "order/main";
	}
	
	/**
	 * 获取商品分类的数据
	 */
	@RequestMapping(value="/order/search", method = RequestMethod.POST)
	@ResponseBody
	public Object getOrderInfoList() {
		PageData pd = this.getPageData();
		
		String page = pd.getString("page");
		String rows = pd.getString("rows");
		String updateTime = pd.getString("order_create_time");
		String orderName = pd.getString("order_name");
		String goodsName = pd.getString("goods_name");
		// 组装查询参数
		Map<String, Object> mapParams = new HashMap<String, Object>(5);
		mapParams.put("page", page);
		mapParams.put("rows", rows);
		mapParams.put("updateTime", updateTime);
		mapParams.put("orderName", orderName);
		mapParams.put("goodsName", goodsName);
		
		PageEntity<OrderInfoResultBean> result = orderService.getOrderListPage(mapParams);
		return result;
	}
	
	/**
	 * 获取商品订单详细信息的数据
	 * 
	 * @return
	 */
	@RequestMapping(value="/order/orderDetailList/{orderId}", method = RequestMethod.POST)
	@ResponseBody
	public Object getOrderDetailInfoList(@PathVariable String orderId) {
		Map<String, Object> params = new HashMap<String, Object>(1);
		params.put("orderId", orderId);
		List<OrderDetailResultBean> bean = orderService.getOrderDetailInfo(params);
		return bean;
	}
	
	/**
	 * 跳转至新增培训信息管理编辑窗口detail.jsp
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value="/order/add")
	public ModelAndView add() {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("mode", "0");
		mv.setViewName("order/detail");
		return mv;
	}
	
	/**
	 * 跳转至新增培训信息管理编辑窗口detail.jsp
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value="/order/showAddress")
	public ModelAndView address() {
		PageData pd = this.getPageData();
		String address_id = pd.getString("address_id");
		ModelAndView mv = this.getModelAndView();
		mv.addObject("s_address_id", address_id);
		mv.setViewName("order/address");
		return mv;
	}
	
	/**
	 * 跳转至新增培训信息管理编辑窗口detail.jsp
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value="/order/update/{orderId}")
	public ModelAndView update(@PathVariable String orderId) {
		ModelAndView mv = this.getModelAndView();
		// 更新
		mv.addObject("mode", "1");
		mv.addObject("order_id", orderId);
		mv.setViewName("order/detail");
		return mv;
	}
	
	/**
	 * 获取编辑的订单数据
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/order/getEditOrderInfoData/{orderId}")
	@ResponseBody
	public Object getEditOrderInfoData(@PathVariable String orderId) {
		OrderEditPageBean rslt = orderService.getOrderInfo(orderId);
		return HttpResult.createSuccessResult(rslt, "0");
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/order/saveOrUpdate")
	@ResponseBody
	public  Object saveOrUpdate () throws IllegalAccessException, InvocationTargetException {
		PageData pd = this.getPageData();
		OrderPageParam bean = new OrderPageParam();
		String order_id = pd.getString("orderId");
		if(!StringUtils.isEmpty(order_id)) {
			bean.setOrderId(Integer.valueOf(order_id));
		}
		
		bean.setOrderName(pd.getString("orderName"));
		if(!StringUtils.isEmpty(pd.getString("totalPrice"))) {
			bean.setTotalPrice(Integer.valueOf(pd.getString("totalPrice")));
		}
		if(!StringUtils.isEmpty(pd.getString("totalPriceJp"))) {
			bean.setTotalPriceJp(Integer.valueOf(pd.getString("totalPriceJp")));
		}
		if(!StringUtils.isEmpty(pd.getString("total_cnt"))) {
			bean.setTotalCnt(Integer.valueOf(pd.getString("total_cnt")));
		}
		if(!StringUtils.isEmpty(pd.getString("freight"))) {
			bean.setFreight(Integer.valueOf(pd.getString("freight")));
		}
		if(!StringUtils.isEmpty(pd.getString("addressId"))) {
			bean.setAddressId(Integer.valueOf(pd.getString("addressId")));
		}
		bean.setUserName(pd.getString("userName"));
		bean.setTelPhone(pd.getString("telPhone"));
		bean.setPostCode(pd.getString("postCode"));
		bean.setAddress(pd.getString("address"));
		bean.setOrderDetailJsonObject(pd.get("orderDetailJsonObject"));
		
		long rslt = 0L;
		if(StringUtils.isEmpty(order_id)) {
			rslt = orderService.insertOrderInfo(bean);
		} else {
			rslt = orderService.updateOrderInfo(bean);
		}
		if(rslt > 0) {
			return HttpResult.createSuccessResult("", "0");
		} else {
			return HttpResult.createErrorResult("创建订单失败！", "", "-1");
		}
	}
	
	/**
	 * 删除商品基本信息
	 * 
	 * @return
	 */
	@RequestMapping(value="/order/del")
	@ResponseBody
	public  Object delete () {
		PageData pd = this.getPageData();
		long rslt = orderService.deleteOrderInfo(Arrays.asList(pd.getString("order_id").split(",")));
		if(rslt > 0) {
			return HttpResult.createSuccessResult("", "0");
		} else {
			return HttpResult.createErrorResult("删除订单失败！", "", "-1");
		}
	}
	
	/**
	 * 获取商品下拉框数据
	 * @return
	 */
	@RequestMapping(value="/order/goodsList")
	@ResponseBody
	public  Object getGoodsComboboxList () {
		List<GoodsComboboxBean> goodsList = orderService.getGoodsComboboxList();
		return HttpResult.createSuccessResult(goodsList, "0");
	}
	
	/**
	 * 根据订单号导出订单文件 
	 * 
	 * @param orderId 订单号
	 */
	@RequestMapping(value="/order/exportExcelFile/{orderId}")
	public void exportExcelFile(@PathVariable String orderId, HttpServletResponse response) {
		orderService.exportExcelFile(orderId, response);
	}
}
